<?php
namespace app\api\controller;
// namespace app\zw_admin\controller;
use \think\Controller;
use think\Db;
// use think\Controller;
use think\Session;
use think\Request;
use Endroid\QrCode\Tests;
use qrcode;
use alisms\SmsDemo;
class BrandController extends Controller
{
    /**
     * 全部品牌列表
     * @return [type] [description]
     */
    public function brand()
    {
        $list = Db::name('brand')
        ->alias('a')
        ->join('drm_class_goods b','b.id = a.class_id')
        ->select();
        // dump($list);
        // exit;
        $brandList = [];
        foreach ($list as $key => &$value) {
            $value['brand_img'] = 'http://www.mochoushop.com/'.$value['brand_img'];
            if ($value['type'] != '1') {
                $brandList[] = $value;
            }
            // $list[] = $value;
          
        }
        if (!empty($list)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$brandList));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }

    /**
     * 全部分类列表
     * @return [type] [description]
     */
    public function class_list()
    {
        $class_liat = db('class_goods')->where(['pid'=>0,'type'=>['neq','1']])->select();
        $class_add[] = [
            'class_name' => '全部',
            'id'         => '0',
            'pid'        => '0'
        ];
        $list = array_merge($class_add,$class_liat);
        
        if (!empty($list)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }



    /**
     * 根据分类展示品牌信息
     * @return [type] [description]
     */
    public function class_brand()
    {
        $class_id = input('param.class_id');
        if ($class_id == 0) {
            $brand_list = db('brand')->where(['type'=>['neq','1']])->select();
        }else{
            $brand_list = db('brand')->where(['class_id'=>$class_id,'type'=>['neq','1']])->select();
        }
        foreach ($brand_list as $key => &$value) {
            $value['brand_img'] = 'http://www.mochoushop.com/'.$value['brand_img'];
            // $list[] = $value;
          
        }
        if (!empty($brand_list)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$brand_list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }


    /**
     * 根据品牌id返回卖场信息
     * @return [type] [description]
     */
    public function selling()
    {
        $brand_id = input('param.brand_id');
        if ($brand_id == 0) {
             $selling_id_list = db('selling_brand')->select();
            $selling_list = db('selling')->where(['type'=>['neq','1']])->select();
        }else{
            $selling_id_list = db('selling_brand')->where(['brand_id'=>$brand_id])->select();
            // dump($selling_id_list);
            $selling_id = '';
            foreach ($selling_id_list as $key => $v) {
                $selling_id .= "`selling_id`="."'".$v['selling_id']."'"." or ";
            }
            $selling_id = rtrim($selling_id,'or ');
            // echo $selling_id;
            $selling_list = db('selling')->where($selling_id)->select();
            foreach ($selling_list as $key => &$value) {
                $value['selling_img'] = 'http://www.mochoushop.com/'.$value['selling_img'];
                // $list[] = $value;
              
            }
        }
        
        // dump($selling_list);
        // exit;
        if (!empty($selling_id_list)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$selling_list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }



    /**
     * 根据卖场id返回品牌信息
     * @return [type] [description]
     */
    public function brand_list()
    {
        $selling_id = input('param.selling_id');
        $brand_id_list = db('selling_brand')->where(['selling_id'=>$selling_id])->select();

        $selling_list = db('selling')->where(['selling_id'=>$selling_id])->find();
        
        // dump($selling_id_list);
        $brand_id = '';
        foreach ($brand_id_list as $key => $v) {
            $brand_id .= "`brand_id`="."'".$v['brand_id']."'"." or ";
        }
        $brand_id = rtrim($brand_id,'or ');
        // echo $selling_id;
        $brand_list = db('brand')->where($brand_id)->select();
        // dump($brand_list);
        // exit;
        $brand_arr = [];
        foreach ($brand_list as $key => $value) {
            $brand_arr['selling_img'] = 'http://www.mochoushop.com/'.$selling_list['selling_img'];
            $brand_arr['content'] = $selling_list['content'];
            $brand_arr['list'][] = $value;
            
        }
        foreach ($brand_arr['list'] as $key => &$value) {
            $value['brand_img'] = 'http://www.mochoushop.com/'.$value['brand_img'];
            // $list[] = $value;
          
        }

        // dump($brand_arr);
        // exit;
        if (!empty($brand_arr)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$brand_arr));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }


    /**
     * 返回商户信息
     * @return [selling_id] [卖场id]
     * @return [brand_id]   [品牌id]
     */
    public function seller()
    {
        $selling_id = input('param.selling_id');
        $brand_id = input('param.brand_id');
        $seller_list = db('seller')->where(['selling_id'=>$selling_id,'brand_id'=>$brand_id])->select();
        $selling_list = db('selling')->where(['selling_id'=>$selling_id])->select();
        $seller_selling = [];
        foreach ($seller_list as $key => $value) {
            $seller_selling['seller'] = $value;
            unset($seller_selling['seller']['username']);
            unset($seller_selling['seller']['pwd']);
        }
        foreach ($selling_list as $key => $value) {
            $seller_selling['selling'] = $value;
        }

        // dump($seller_selling);
        // exit;
        // $seller_selling = array_merge($seller_list,$selling_list);
        // dump($seller_selling);
        // exit;
        // unset($seller_list[0]['username']);
        // unset($seller_list[0]['pwd']);

        if (!empty($seller_selling)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$seller_selling));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }


    /**
     * 品牌模糊查询
     * @return [brand_name] [品牌名字]
     */
    public function search()
    {
        $brand_name = input('param.brand_name');
        $where['brand_name'] = array('like','%'.$brand_name.'%');
        $brand_list = db('brand')->where($where)->select();
        foreach ($brand_list as $key => &$value) {
            $value['brand_img'] = 'http://www.mochoushop.com/'.$value['brand_img'];
            // $list[] = $value;
          
        }
        // dump($brand_list);
        // exit;
        if (!empty($brand_list)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$brand_list));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }






    /**
     * 卖场模糊查询
     * @return [brand_name] [品牌名字]
     */
    public function selling_search()
    {
        $selling_name = input('param.selling_name');
        $where['selling_name'] = array('like','%'.$selling_name.'%');
        $selling_list = db('selling')->where($where)->select();
        // dump($selling_list);
        // exit;
        $selling_arr = [];
        foreach ($selling_list as $key => &$value) {
            $selling_arr[$key]['selling_id'] = $value['selling_id'];
            $selling_arr[$key]['selling_name'] = $value['selling_name'];
          
        }
        // dump($selling_arr);
        // exit;
        if (!empty($selling_arr)) {
            echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$selling_arr));
            exit;
        }else{
            echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
            exit;
        }
    }



    

    // /**
    //  * 全部分类列表
    //  * @return [type] [description]
    //  */
    // public function class_index()
    // {
    //     $class_list = db('class_goods')->where(['pid'=>0])->select();
    //     $class_lei = db('class_goods')->where(['pid'=>['neq','0']])->select();
    //     // echo '111';
    //     // dump($class_lei);
    //     // exit;
    //     // $list = [];
    //     foreach ($class_list as $k => $v) {
    //          $list[$k] = $v;
    //     }
    //     foreach ($list as $ky => $vo) {
    //         $res[] = $vo;
    //         foreach ($class_lei as $ke => $vv) {
    //             $arry['class_name'] = $vv['class_name'];
    //             $arry['class_id'] = $vv['id'];
    //             if ($vo['id'] == $vv['pid']) {
    //                 $res[$ky]['class_list'][] = $arry;
    //                 // $res[$ky]['data'][] = $vv['id'];
    //             }
    //             // dump($vo['class_name']);
    //         }
    //            // dump($list[$k]);
    //            // dump($vo['class_name']);
    //     }
    //     // dump($res);
    //     if (!empty($res)) {
    //         echo json_encode(array("status"=>1,'message'=>'请求成功!',"data"=>$res));
    //         exit;
    //     }else{
    //         echo json_encode(array("status"=>2,'message'=>'请求失败!',"data"=>[]));
    //         exit;
    //     }
    // }
    
}
